package com.hundsun.yr.universal.p2p.ui.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.ui.view.ProgressDailog;

import de.greenrobot.event.EventBus;

/**
 * 法大大页面
 */
public class WebFDDActivity extends BaseMineWebActivity {
    /**
     * 支持放大缩小
     */
    public static final String supportZoom = "supportZoom";
    private boolean supportZoomFlag;
    private Handler handler;
    /** 加载进度条 **/
    protected ProgressDailog progressDailog = null;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        supportZoomFlag = extras.getBoolean(supportZoom);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        progressDailog = new ProgressDailog(this, R.style.progress_dialog);
        toolbar_title.setText(mWebTitle);
        if (supportZoomFlag) {
            mBrowserLayout.mWebView.getSettings().setSupportZoom(supportZoomFlag);
            mBrowserLayout.mWebView.getSettings().setBuiltInZoomControls(supportZoomFlag);
        }
        mBrowserLayout.mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains(getString(R.string.callback_url))) {
                    //只要修改成功直接跳转到设置中心页面，其他设置页面应该关闭
                    if (BaseAppManager.getInstance().getActivity(IWantLoanActivity.class) != null) {
                        BaseAppManager.getInstance().getActivity(IWantLoanActivity.class).finish();
                    }

                    if (BaseAppManager.getInstance().getActivity(WebFDDProtocolActivity.class) != null) {
                        BaseAppManager.getInstance().getActivity(WebFDDProtocolActivity.class).finish();
                    }
                    //一秒钟再发出去
                    handler = new Handler();
                    progressDailog.showProgress();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            progressDailog.dismiss();
                            EventBus.getDefault().post(new EventCenter(Extras.LOAN_ORDER_SUCCESS));//登陆成功通知某些页面进行数据刷新
                            //跳转至我的借款页面
                            readyGoThenKill(MineLoanActivity.class);
                        }
                    },1000);
                } else {
                    mBrowserLayout.mWebView.loadUrl(url);
                }

                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 网页 返回
     */
    public void webBack() {
        if (mBrowserLayout.canGoBack()) {
            mBrowserLayout.goBack();
        } else {
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            webBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
